import os.path
import time
import unittest
from selenium import webdriver
from selenium.common.exceptions import NoAlertPresentException
class testCase1(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.url="https://www.baidu.com/"
        self.driver.maximize_window()
        time.sleep(3)

    def tearDown(self):
        self.driver.quit()
    @unittest.skip("skipping")
    def test_baidu1(self):
        driver=self.driver
        driver.get(self.url)
        driver.find_element_by_id("kw").send_keys("李易峰")
        driver.find_element_by_id("su").click()
        time.sleep(3)
        #self.assertEqual(1==2,msg="not equal")


    def test_baidu2(self):
        driver = self.driver
        self.driver.get(self.url)
        # 判断是否打开百度页面
        try:
            self.assertEqual(driver.title,"你就知道",msg="判断失败，没有打开百度页面")

        except:
            self.save_error_image(driver,"baidu.png")
        time.sleep(3)

    

    def save_error_image(self,driver,name):
        if not os.path.exists("./errorImage"):
            os.makedirs("./errorImage")
        now = time.strftime("%Y%m%d-%H%M%S",time.localtime(time.time()))
        driver.get_screenshot_as_file("./errorImage/"+now+"-"+name)



    # def is_alert_exist(self):
    #     try:
    #         self.driver.switch_to.alert
    #     except NoAlertPresentException as e:
    #         return False
    #     return True
    #
    # if __name__ == "__main__":
    #     unittest.main(verbosity=0)